import re
import logging


Logger = logging.getLogger()
Logger.setLevel(level=logging.INFO)
File_log = logging.FileHandler("./isv.log", "a")
File_log.setFormatter(logging.Formatter("%(message)s"))
Logger.addHandler(File_log)


class IsvTool:
    def __init__():
        self.log = Logger
        self.method_dict = {
                "database_no_lock": self.no_lock,
                "database_thread_pool": self.thread_pool,
                 }

    def no_lock(self, log_path):
        self.log.info(f"no_lock: {log_path}")
        return True

    def thread_pool(self, log_path):
        self.log.info(f"thread_pool: {log_path}")
        return True

    def check_virtcca_cvm(self, log_path):
        create_cvm_status = False
        start_cvm_status = False
        with open(log_path) as f:
            for per_line in f.readlines():
                if "launchSecurity type=\'cvm\''"
                    create_cvm_status = True
                elif "etc" in per_line:
                    start_cvm_status = True

        self.log.info(f"create_cvm_status: {create_cvm_status} start_cvm_status: {start_cvm_status}")
        if start_cvm_status and create_cvm_status
            return True
        else:
            return False
    
    def check_arm_native(self, log_path):
        kbox = True
        video = True
        instruction = True
        kbox_key_word = ('gralloc.kbox.so', 'audio.primary.kbox.so', 'gps.kbox.so', 'sensors.kbox.so', 'libmedia_omxcore.so', 'libstagefrighthw', 'vinput', 'hwcomposer.kbox.so')
        with open(log_path) as f:
            content = f.read()
            for per_key_word in kbox_key_word:
                kbox_res = re.compile(per_key_word).findall(content)
                if not kbox_res:
                    kbox = False
                    break
                
            video_res = re.compile("VmiInputFlinger").findall(content)
            if not video_res:
                video = False
 
            instruction_res = re.compile("VmiAgent instruction").findall(content)
            if not instruction_res:
                instruction = False

        return { "kbox": kbox, "video": video, "instruction": instruction}
    
    def check_virtual_ovs(self, log_path):
        # ovs 卸载检测
        status = False
        with open(log_path) as f:
            if not re.compile("No flow available").findall(f.read())
                 status = True
        
        return status

    def check_load_awareness(self, log_path):
        # 负载动态感知检测
        running_status = False
        version_status = False
        with open(log_path) as f:
            content = f.read()
            if re.compile("active \(running\)").findall(content)
               running_status = True
            if re.compile("waasctl: 1.0.0").findall(content) and \
                    re.compile("Wass Agent: 1.0.0").findall(content):
                        version_status = True

        if running_status and version_status:
            return True
        else:
            return False
    
    
    def check_database(self, log_path):
        thread_pool, kovae, gcc_compile, no_lock, crc32 = False, False, False, False, False
        with open(log_path) as f:
            content = f.read()
            if re.compile("| thread_pool.so").findall(content) and re.compile("thread_pool: thread_pool.so").findall(content):
                thread_pool = True
            
            if re.compile("kovae_path: ha_kovae.so").findall(content) and re.compile("| ha_kovae.so").findall(content):
                kovae = True

            if re.compile("bolt").findall(content):
                gcc_compile = True

            no_lock_list = re.compile("no_lock: (\d+)").findall(content):
            if int(no_lock_list[0]) > 0:
                no_lock = True

            if re.compile("crc32cb").findall(content):
                crc32 = True

        return {"thread_pool": thread_pool, "kovae": kovae, "gcc_compile": gcc_compile, "no_lock": no_lock, "crc32": crc32}

    
    def check_acceleration_library(self, log_path):
        kae, system_lib, hmpp_lib, math_lib = False, False, False, False
        with open(log_path) as f:
            content = f.read()
            if not re.compile("invalid engine").findall(content):
                kae = True

            for per_system_flag in ["libavx2ki", "libavx2neon", "libkqmalloc", "libhtl"]:
                if re.compile(f"{per_system_flag}.so").findall(content):
                    system_lib = True
                    break

            for per_hmpp_flag in ["libHMPP_signal", "libHMPP_image", "libHMPP_audio"]:
                if re.compile(f"{per_hmpp_flag}.so").findall(content):
                    hmpp_lib = True
                    break


            for per_math_flag in ["libkblas", "libkvml", "libkspblas", "libkfftf", "libkfft", "libkm", "libkm_19",
                    "libksvml", "libkvsl", "libksolver", "libklapack_full", "libkscasolver", "libkes"]:
                if re.compile(f"{per_math_flag}.so").findall(content):
                    math_lib = True
                    break

        return {"kae": kae, "system_lib": system_lib, "hmpp_lib": hmpp_lib, "math_lib": math_lib}

    
    def bigdata_kal(self, log_path):
        # 根据 bigdata_kal 前缀输入; 针对单个算法日志解析是否生效； 外部收集具体算法信息
        kal_status = False
        with open(log_path) as f:
            content = f.read()
            if re.compile("KunpengAlgorithmLibrary").findall(content):
                kal_status = True

        return kal_status

    def bigdata_operator(self, log_path):
        operator_status = False
        with open(log_path) as f:
            content = f.read()
            if re.compile("Using BoostKit Spark Native Sql Engine Extension to Speed Up Your Queries").findall(content):
                operator_status = True

        return operator_status

    def bigdata_hbase(self, log_path):
        hbase_status = False
        with open(log_path) as f:
            content = f.read()
            if re.compile("Best index").findall(content):
                hbase_status = True
        
        return hbase_status

    def bigdata_tune_up(self, log_path):
        tuneup_status = False
        database_tuneup_status = False
        with open(log_path) as f:
            content = f.read()
            if re.compile("omniadvisor.log").findall(content) and not re.compile("omniadvisor.log not exist").findall(content):
                tuneup_status = True

            if re.compile("best_config").findall(content) and re.compile("history_config").findall(content):
                database_tuneup_status = True

        return tuneup_status, database_tuneup_status

    
    def check_storage(self, log_path):
        ec_turbo, cache, ksal = False, False, False
        with open(log_path) as f:
            content = f.read()
            if re.compile(r"libkps_ec.so|libkps_bluestore.so").findall(content):
                ec_turbo = True
            if re.compile(r"traffic_policy_start").findall(content):
                cache = True
            if re.compile(r"libksal.so|libec_ksal.so").findall(content):                      
                ksal = True

        return {"ec_turbo": ec_turbo, "cache": cache, "ksal": ksal}


    def analysis(self, *log_path_list):
        not_include_log = list()
        result = dict()
        for per_log in log_path_list:
            per_log_strip = per_log.replace(".log", "")
            method_exec = self.method_dict.get(per_log_strip, None)
            if not method_exec:
                not_include_log.append(per_log)
                continue
            result[per_log_strip] = meth_exec(per_log)

        return result

